package com.wuad.platformwuad.module.study.collection;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * list去重
 *
 * @author ：Wuad
 * @date ：Created in 2021/3/1 17:18
 * @description :
 */
@Slf4j
public class ListQuchongDemo {


    /**
     * 对象类型的list去重，根据实体类里某个字段去重
     * @param args
     */
    public static void main(String[] args) {

        List<User1Entity> userEntityList = new ArrayList<User1Entity>();
        User1Entity userEntity1 = new User1Entity("张三", "001");
        userEntityList.add(userEntity1);
        User1Entity userEntity2 = new User1Entity("李四", "001");
        userEntityList.add(userEntity2);
        User1Entity userEntity3 = new User1Entity("王五", "002");
        userEntityList.add(userEntity3);
        for (User1Entity u : userEntityList) {
            System.out.println(u.getName());
        }
        System.out.println("去重后=======>");
        List<User1Entity> userEntityListNoDupAndSort = removeDuplicateUser(userEntityList);
        for (User1Entity u : userEntityListNoDupAndSort) {
            System.out.println(u);
        }
    }
    private static ArrayList<User1Entity> removeDuplicateUser(List<User1Entity> userEntities) {
        Set<User1Entity> set = new TreeSet<User1Entity>(new Comparator<User1Entity>() {
            @Override
            public int compare(User1Entity o1, User1Entity o2) {
                //字符串,则按照asicc码升序排列
                return o1.getMsg().compareTo(o2.getMsg());
            }
        });
        set.addAll(userEntities);
        return new ArrayList<User1Entity>(set);
    }

    /**
     * 两个list取交集和剔除元素
     *
     * @throws Exception
     */
    @Test
    public void Caozuo() throws Exception {

        List<String> list1 = new ArrayList<>();
        list1.add("name");
        list1.add("age");

        List<String> list2 = new ArrayList<>();
        list2.add("name");
        list2.add("age");
        list2.add("333");

        Collection exists=new ArrayList<>(list2);
        Collection notexists=new ArrayList<>(list2);

        exists.removeAll(list1);
        System.out.println("list2中不存在于list1中的："+exists);
        notexists.removeAll(exists);
        System.out.println("list2中存在于list1中的："+notexists);
        StringBuffer stringBuffer = new StringBuffer();
        for (Object ss : notexists) {
            stringBuffer.append("\"" + ss + "\",");
        }
        System.out.println(stringBuffer);

    }


    /**
     * 对象集合去重
     */
    public static class list_quchong_1{
        public static void main(String[] args) {
            List<User1Entity> userEntityList = new ArrayList<User1Entity>();
            User1Entity userEntity1 = new User1Entity("张三", "001");
            userEntityList.add(userEntity1);
            User1Entity userEntity2 = new User1Entity("李四", "001");
            userEntityList.add(userEntity2);
            User1Entity userEntity3 = new User1Entity("王五", "002");
            userEntityList.add(userEntity3);

            List<User1Entity> user1Entities = new ArrayList<>();
            userEntityList.stream().filter(distinctByKey(p -> p.getMsg()))  //filter保留true的值
                    .forEach(user1Entities::add);
            System.out.println(user1Entities);
        }


        static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
            Map<Object,Boolean> seen = new ConcurrentHashMap<>();
            //putIfAbsent方法添加键值对，如果map集合中没有该key对应的值，则直接添加，并返回null，如果已经存在对应的值，则依旧为原来的值。
            //如果返回null表示添加数据成功(不重复)，不重复(null==null :TRUE)
            return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
        }
    }
}

